package org.brownflat.lighthouse.ci.hudson;

import org.brownflat.lighthouse.ci.Builder;
import org.brownflat.lighthouse.ci.Builds;
import org.brownflat.lighthouse.ci.ConnectionException;
import org.brownflat.lighthouse.ci.HTTPServer;
import org.brownflat.lighthouse.ci.Monitorable;

public class HudsonServer extends HTTPServer implements Monitorable {

    private String view = "";
    private String name = "HudsonServer";
    private Builder<Builds, String> builder = new HudsonBuilder();

	public HudsonServer(String host, int port) {
		super(host, port);		
	}

    public HudsonServer(String host, int port, String view) {
		super(host, port);
        setView(view);
	}

    public HudsonServer() {
        super("localhost", 8080);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getSummary() {
        return String.format("%s - %s", name, getURL());
    }

    public void setView(String view) {
        this.view = view;
    }

    @Override
    public Builds builds() {
        try {
			String result = execute(new HudsonLatestBuildsRequest(view));
			return builder.build(result);
		} catch (ConnectionException e) {
			throw new RuntimeException(e);
		}
    }
}
